﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public static class MethodExtension
    {
        public static void InitToSameValue(this int[,] a,int v)
        {
            int h = a.GetLength(0);
            int w = a.GetLength(1);
            for (int i = 0, j = h - 1; i <= j;i++,j--)
            {
                for (int m = 0; m<w; m++)
                {
                    a[i,m]= v;
                    a[j, m] = v;
                }
            }
        }


        /// <summary>
        /// 二分法查找
        /// </summary>
        /// <param name="a"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        public static bool HasAValue(this int[] a,int v)
        {
            return BinarySearch(a,v,0,a.Length-1);
        }

        public static bool BinarySearch(int[] a,int goal,int startIndex,int endIndex)
        {                   
            if (startIndex <= endIndex)
            {
                int mid = (endIndex + startIndex) / 2;
                if (a[mid] == goal)
                    return true;

                return BinarySearch(a, goal, startIndex, mid - 1) || BinarySearch(a, goal, mid + 1, endIndex);
            }

            return false;

        }
    }
}
